import { Injectable } from '@nestjs/common';
import { Status } from 'src/dictionaries/index.enum';
import { InjectRepository } from '@nestjs/typeorm';
import { BannerEntity } from 'src/modules/admin/banner/entities/banner.entity';
import { throwHttp } from 'src/utlis';
import { Repository } from 'typeorm';

@Injectable()
export class BannerService {
  constructor(
    @InjectRepository(BannerEntity)
    private bannerEntityRepository: Repository<BannerEntity>,
  ) { }
  async getBannerList(lang) {
    try {
      const data = await this.bannerEntityRepository.find({
        where: {
          status: Status.ENABLED,
          lang
        },
        cache: true,
        order: { sort: 'DESC' },
        relations: ['movies', 'movies.tags', 'movies.category']
      });
      return data.map(item => {
        return {
          ...item,
          movies: {
            ...item.movies,
            tags: item?.movies?.tags?.map(tag => tag.name),
            category: item?.movies?.category?.map(cat => cat.name),
          }
        }
      })
    } catch (error) {
      throwHttp(error.message);
    }
  }
}
